function Node(element) {
    this.element = element;
    this.next = null;
}

function LinkList() {
    this.head = new Node("head");
    this.find = find;       //查找返回元素节点
    this.findPrevious = findPrevious;       //查找返回上一个元素节点
    this.insert = insert;       //插入元素
    this.remove = remove;       //删除元素
    this.display = display;     //打印显示元素
}

const find = function (item) {
    let currentNode = this.head;
    while(currentNode.element !== item) {
        currentNode = currentNode.next;
    }
    return currentNode;
}

const findPrevious = function (item) {
    let currentNode = this.head;
    let previousNode = null;
    while(currentNode.element !== item) {
        previousNode = currentNode;
        currentNode = currentNode.next;
    }
    return previousNode;
}

const insert = function (element, item) {
    const insertElement = this.find(element);
    const nextElement = insertElement.next;
    const node = new Node(item);
    node.next = nextElement;
    insertElement.next = node;
}

const remove = function (element) {
    const removeElement = this.find(element);
    const previousNode = this.findPrevious(element);
    previousNode.next = removeElement.next;
}


const display = function () {
    let outputLine = "";
    let positionNode = this.head;
    while(positionNode !== null) {
        outputLine += positionNode.element + "->"
        positionNode = positionNode.next;
    }
    console.log(outputLine);
}


const linkList = new LinkList();
linkList.insert("head", "test1");
linkList.insert("head", "test2");
linkList.display();
linkList.remove("test2");
linkList.display();
linkList.remove("test1");
linkList.display();